import { createApp } from 'vue'
// 全局
// import ElementPlus from 'element-plus'
// import 'element-plus/dist/index.css'
// import zhCn from 'element-plus/es/locale/lang/zh-cn'
// 按需载入
import 'element-plus/dist/index.css'
import { components, plugins } from '@/plugins/element'

import * as Icons from '@element-plus/icons'
import i18n from '@/language/index'
import { toLine } from './utils/toLine'
import Router from '@/router'
import Vuex from '@/store'
// import svgIcon from '@/components/svgIcon.vue'
import Image from '@/components/global/Image.vue'
import initDirective from '@/utils/directive'
import validUtils from '@/utils/validUtils'
import MetaInfo from 'vue-meta-info'
// @ts-ignore
import App from '@/App.vue'
  
// 要登录的路由
const loginList = ['/cart', '/center', '/buy']

Router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    // @ts-ignore
    document.title = to.meta.title // 根据路由动态显示页面title
  }
  let flag = loginList.findIndex(item => to.path.indexOf(item) >= 0)
  if (flag >= 0) {
    const loginUserObjStr = localStorage.getItem('loginUserObj')
    let loginUserObj = loginUserObjStr && loginUserObjStr != null ? JSON.parse(loginUserObjStr) : undefined
    if(loginUserObj && loginUserObj.userId) {
      next()
    }else{
      next({ path: '/login' })
    }
  }else{
    next()
  }
})

const app = createApp(App)
// 全局
// app.use(ElementPlus, {
//   locale: zhCn
// })
// 按需导入Element Plus组件和插件
components.forEach(component => {
  app.component(component.name, component)
})
plugins.forEach(plugin => {
  app.use(plugin)
})

// el-icon-xxx
for (const i in Icons) {
  app.component(`el-icon${toLine(i)}`, (Icons as any)[i])
}
// 验证工具类
app.config.globalProperties.validUtils = validUtils
app.provide('validUtils', validUtils)

// 加载自定义指令
initDirective(app)
app.use(Router)
app.use(MetaInfo)
app.use(Vuex)
app.use(i18n)
// app.component('SvgIcon', svgIcon)
app.component('Image', Image)
app.mount('#app')
